package com.opdar.athena.web.controllers;

import com.opdar.athena.base.Constants;
import com.opdar.athena.base.RedisSessionManager;
import com.opdar.athena.entities.ErrorResult;
import com.opdar.athena.entities.ProjectEntity;
import com.opdar.athena.entities.TokenEntity;
import com.opdar.athena.entities.UserEntity;
import com.opdar.athena.web.base.Result;
import com.opdar.athena.web.service.AppCheckService;
import com.opdar.athena.web.service.UserService;
import com.opdar.platform.annotations.Request;
import com.opdar.platform.core.session.ISessionManager;
import com.opdar.platform.utils.MD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * Created by shiju on 2017/7/10.
 */
@Controller
public class AppController {

    @Autowired
    AppCheckService appCheckService;
    @Autowired
    UserService userService;
    @Autowired
    RedisSessionManager sessionManager;

    @Request(value = "/register", format = Request.Format.JSON)
    public Object register(String appId,Long timestamp,String signature,String userName,String userPwd) {
        HashMap<String,Object> parameters = new HashMap<String, Object>();
        parameters.put("userName",userName);
        parameters.put("userPwd",userPwd);
        parameters.put("timestamp",timestamp);
        ErrorResult errorResult = appCheckService.authApp(parameters, appId, signature);
        if(errorResult.getCode() == 0){
            //注册用户
            UserEntity user = userService.register(userName, userPwd, appId);
            if(user != null){
                //注册成功
                return user;
            }else{
                errorResult = new ErrorResult(Constants.ErrorCode.USER_IS_EXIST);
            }
        }
        return errorResult;
    }

    @Request(value = "/changePwd", format = Request.Format.JSON)
    public Object changePwd(String appId,String signature,Long timestamp,String userName,String userPwd) {
        HashMap<String,Object> parameters = new HashMap<String, Object>();
        parameters.put("userName",userName);
        parameters.put("userPwd",userPwd);
        parameters.put("timestamp",timestamp);
        ErrorResult errorResult = appCheckService.authApp(parameters, appId, signature);
        if(errorResult.getCode() == 0){
            UserEntity user = userService.changePwd(userName, userPwd, appId);
            if(user != null){
                return user;
            }else{
                errorResult = new ErrorResult(Constants.ErrorCode.USER_NOT_EXIST);
            }
        }
        return errorResult;
    }

    @Request(value = "/check", format = Request.Format.JSON)
    public Object check(String appId,String signature,Long timestamp,String token) {
        HashMap<String,Object> parameters = new HashMap<String, Object>();
        parameters.put("token",token);
        parameters.put("timestamp",timestamp);

        ErrorResult errorResult = appCheckService.authApp(parameters, appId, signature);
        if(errorResult.getCode() == 0){
            //认证
            boolean ret = sessionManager.exist(token);
            if(!ret){
                return new ErrorResult(Constants.ErrorCode.TOKEN_IS_EXPIRE);
            }
            sessionManager.clearTimeout(token);
        }
        return errorResult;
    }

    @Request(value = "/login", format = Request.Format.JSON)
    public Object login(String appId,String signature,Long timestamp,String userName,String userPwd,Integer platform) {
        HashMap<String,Object> parameters = new HashMap<String, Object>();
        parameters.put("userName",userName);
        parameters.put("userPwd",userPwd);
        parameters.put("platform",platform);
        parameters.put("timestamp",timestamp);

        ErrorResult errorResult = appCheckService.authApp(parameters, appId, signature);
        if(errorResult.getCode() == 0){
            //认证
            UserEntity user = userService.authByUserName(userName, userPwd, appId);
            if(user != null){
                String token = UUID.randomUUID().toString();
                user.setPlatform(platform);
                sessionManager.set(token,user, Constants.SESSION_TIMEOUT);
                Map<String,Object> result = new HashMap<String,Object>();
                result.put("token",token);
                result.put("user",user);
                return result;
            }else{
                errorResult = new ErrorResult(Constants.ErrorCode.USER_AUTH_FAILURE);
            }
        }
        return errorResult;
    }

}
